package controller;


import com.google.gson.Gson;
import pojo.Enterprise;
import pojo.User;
import service.EnterpriceService;
import service.UserService;
import service.impl.EnterpriceServiceimpl;
import service.impl.UserServiceimpl;
import util.PageHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.awt.print.Book;
import java.io.IOException;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
import java.util.ArrayList;

@WebServlet("/book/*")
public class Servlet extends HttpServlet {
    UserService userService = new UserServiceimpl();
    EnterpriceService enterpriceService = new EnterpriceServiceimpl();
    @Override
    protected void service(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        //0.处理乱码：
        request.setCharacterEncoding("utf-8");
        response.setCharacterEncoding("utf-8");
        response.setContentType("application/json;charset=utf-8");

        //1.获得请求路径
        String url = request.getRequestURI();
        String methodName = url.substring(url.lastIndexOf("/") + 1);
        Class cla = Servlet.class;
        try {
            Method method = cla.getMethod(methodName, HttpServletRequest.class, HttpServletResponse.class);
            method.invoke(cla.newInstance(), request, response);
        } catch (NoSuchMethodException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (InvocationTargetException e) {
            e.printStackTrace();
        }
    }

    public void query1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {


    }

    //按条件查询数据
    public void query2(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        EnterpriceService enterpriceService=new EnterpriceServiceimpl();
        String uname = request.getParameter("uname");
        String uid = request.getParameter("uid");
        String email = request.getParameter("email");
        String phone = request.getParameter("phone");
        String val = request.getParameter("val");
        String scale = request.getParameter("scale");
        String qq = request.getParameter("qq");
        Enterprise enterprise = new Enterprise();
        enterprise.setName(uname);
        enterprise.setSocial_uniform_code(uid);
        enterprise.setEmail(email);
        enterprise.setPhone(phone);
        enterprise.setAddress(val);
        enterprise.setScale(scale);
        enterprise.setFax(qq);
        int i = enterpriceService.insertEnterprise(enterprise);
        boolean b = i == 0 ? false : true;
        Gson gson = new Gson();
        response.getWriter().println(gson.toJson(b));

    }

    public void add1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String email1=request.getParameter("email");
        String password1=request.getParameter("password");

        //准备参数：
        User user=new User();
        user.setAccount(email1);
        user.setPassword(password1);

        ArrayList list = userService.selectUsername(user);
        boolean b = list.size() > 0 ? true : false;
        Gson gson=new Gson();
        String s = gson.toJson(b);
        response.getWriter().println(s);

    }

    public void delete1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {



    }



    public void update1(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {

    }

    public void select1(HttpServletRequest request, HttpServletResponse response) throws IOException {
        String bid = request.getParameter("bid");
        ArrayList<User> list = userService.selectByIdcard(bid);
        String eid = null;
        Enterprise enterprise=new Enterprise();
        for (User u:list
             ) {
             eid = u.getEnterprise_id();
        }
        enterprise.setId(Integer.parseInt(eid));
        boolean b = list.size() == 0 ? false : true;
        if(b) {
            Enterprise list1 = enterpriceService.selectEnterpriseById(enterprise);
            Gson gson = new Gson();
            response.getWriter().println(gson.toJson(list1));
        }else{

        }
    }

}